<template>
  <div>
    <el-input placeholder="选择试卷" @focus="handleShowModal" v-model="title" />
    <!-- 选择试卷弹出框 -->
    <el-dialog
      title="选择试卷"
      :visible.sync="open"
      width="900px"
      append-to-body
    >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="试卷标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入试卷标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="编程语言" prop="lang">
          <el-select
            v-model="queryParams.lang"
            placeholder="请选择编程语言"
            clearable
          >
            <el-option
              v-for="dict in dict.type.QuestionLang"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="试卷等级" prop="tagLevel">
          <el-select
            v-model="queryParams.tagLevel"
            placeholder="请选择试卷等级"
            clearable
          >
            <el-option
              v-for="dict in dict.type.ExamTagLevel"
              :key="dict.raw.dictCode"
              :label="dict.label"
              :value="dict.raw.dictCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="试卷来源" prop="tagFrom">
          <el-select
            v-model="queryParams.tagFrom"
            placeholder="请选择试卷来源"
            clearable
          >
            <el-option
              v-for="dict in dict.type.ExamTagFrom"
              :key="dict.raw.dictCode"
              :label="dict.label"
              :value="dict.raw.dictCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="试卷年份" prop="tagYear">
          <el-select
            v-model="queryParams.tagYear"
            placeholder="请选择试卷年份"
            clearable
          >
            <el-option
              v-for="dict in dict.type.ExamTagYear"
              :key="dict.raw.dictCode"
              :label="dict.label"
              :value="dict.raw.dictCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="paperList">
        <el-table-column label="试卷" width="300" align="center" prop="title">
        </el-table-column>
        <el-table-column label="满分" align="center" prop="score" />
        <el-table-column label="题目数量" align="center" prop="qcount" />
        <el-table-column
          label="建议用时(分钟)"
          align="center"
          prop="suggesttime"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="primary" @click="handleClickPaper(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listPaper } from '@/api/exam/paper';

export default {
  name: 'ExamPaperSelect',
  dicts: ['QuestionLang', 'ExamTagLevel', 'ExamTagFrom', 'ExamTagYear'],
  props: ['paperTitle'],
  watch: {
    paperTitle(val) {
      this.title = val;
    },
  },
  data() {
    return {
      open: false,
      loading: false,
      paperId: null,
      title: '',
      queryParams: {
        title: '',
        lang: null,
        tagLevel: null,
        tagFrom: null,
        tagYear: null,
        tags: '',
      },
      // 表单参数
      form: {},
      // 总条数
      total: 0,
      paperList: [],
    };
  },
  created() {
    this.getList();
    this.title = this.paperTitle;
  },
  methods: {
    /** 查询考试试卷列表 */
    getList() {
      this.loading = true;
      let tags = [];
      this.queryParams.tagFrom && tags.push(this.queryParams.tagFrom);
      this.queryParams.tagLevel && tags.push(this.queryParams.tagLevel);
      this.queryParams.tagYear && tags.push(this.queryParams.tagYear);
      this.queryParams.tags = tags.join(',');
      listPaper(this.queryParams).then((response) => {
        this.paperList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        title: null,
        lang: null,
        tagLevel: null,
        tagFrom: null,
        tagLevel: null,
        tags: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    handleClickPaper(paper) {
      this.open = false;
      this.title = paper.title;
      this.$emit('input', paper.paperId);
    },
    handleShowModal() {
      this.open = true;
    },
  },
};
</script>

<style></style>
